package ca.uwaterloo.titslongcall;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import ca.uwaterloo.titslongcall.MainActivity.CallCard;
import ca.uwaterloo.titslongcall.MainActivity.Contact;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class PickCardActivity extends Activity{
	
	public class SPMethods{
		//We Have 4 SharedPreferences: AllofMyContacts, SetofMyContacts, AllofMyCallCards, SetofMyCallCards.
		String SetofMyContacts = "SetofMyContacts";
		String SetofMyCallCards = "SetofMyCallCards";
		String AllofMyContacts = "AllofMyContacts";
		String AllofMyCallCards = "AllofMyCallCards";
		String Name_Set = "Name_Set";
		String Card_Set = "Card_Set";
		String MyDefaultCallCard = "MyDefaultCallCard";
		String DefaultCC = "DefaultCC";
		

		@SuppressLint("NewApi")
		public void saveSharedPreferences(String SPName, String Key, String Value){
			
			SharedPreferences MySP = getSharedPreferences(SPName, MODE_PRIVATE);
			SharedPreferences.Editor editor = MySP.edit();
			
			if (SPName == SetofMyContacts){	//when i wanna edit the set of my contact...(it's for counting)
				Set<String> MySet = MySP.getStringSet(Name_Set, new TreeSet<String>());	//in this set, contacts are stored as name+phone
				Set<String> MyNewSet =  new TreeSet<String>();
				for (String each: MySet){
					MyNewSet.add(each);
				}
				MyNewSet.add(Key+Value);
				editor.putStringSet(Name_Set, MyNewSet);
				editor.commit();
			}
			
			if (SPName == SetofMyCallCards){	//when i wanna edit the set of my callcard...same
				Set<String> MySet = MySP.getStringSet(Card_Set, new TreeSet<String>());
				Set<String> MyNewSet =  new TreeSet<String>();
				for (String each: MySet){
					MyNewSet.add(each);
				}
				MyNewSet.add(Key+Value);
				editor.putStringSet(Card_Set, MyNewSet);
				editor.commit();
			}
			
			if (SPName == AllofMyContacts || SPName == AllofMyCallCards){	//when i wanna add a contact or a callcard
				editor.putString(Key+Value, Key+Value);
				editor.commit();
			}
			
			if (SPName == MyDefaultCallCard){
				editor.putString(DefaultCC, Key+Value);
				editor.commit();
			}
		}
		
		@SuppressLint("NewApi")
		public void removeContact(String ContactInfo){
			SharedPreferences MySP1 = getSharedPreferences(SetofMyContacts, MODE_PRIVATE);
			SharedPreferences.Editor editor1 = MySP1.edit();
			Set<String> myset = loadSharedPreferencesSet(SetofMyContacts, Name_Set);
			Set<String> mynewset = new TreeSet<String>();
			for (String each: myset){
				mynewset.add(each);
			}
			mynewset.remove(ContactInfo);
			editor1.putStringSet(Name_Set, mynewset);
			SharedPreferences MySP2 = getSharedPreferences(AllofMyContacts, MODE_PRIVATE);
			SharedPreferences.Editor editor2 = MySP2.edit();
			editor2.remove(ContactInfo);
			editor1.commit();
			editor2.commit();
		}
		
		@SuppressLint("NewApi")
		public void removeCallCard(String CallCardInfo){
			SharedPreferences MySP1 = getSharedPreferences(SetofMyCallCards, MODE_PRIVATE);
			SharedPreferences.Editor editor1 = MySP1.edit();
			Set<String> myset = loadSharedPreferencesSet(SetofMyCallCards, Card_Set);
			Set<String> mynewset = new TreeSet<String>();
			for (String each: myset){
				mynewset.add(each);
			}
			mynewset.remove(CallCardInfo);
			editor1.putStringSet(Card_Set, mynewset);
			SharedPreferences MySP2 = getSharedPreferences(AllofMyCallCards, MODE_PRIVATE);
			SharedPreferences.Editor editor2 = MySP2.edit();
			editor2.remove(CallCardInfo);
			editor1.commit();
			editor2.commit();
		}
		
		public String loadSharedPreferences(String SPName,String Key){
			SharedPreferences MySP = getSharedPreferences(SPName, MODE_PRIVATE);
			return MySP.getString(Key, "");
		}
		
		@SuppressLint("NewApi")
		public Set<String> loadSharedPreferencesSet(String SPName, String Key){
			SharedPreferences MySP = getSharedPreferences(SPName, MODE_PRIVATE);
			return MySP.getStringSet(Key, new TreeSet<String>());
		}
		
		
		public void addContact(Contact contact){
			saveSharedPreferences(AllofMyContacts, contact.Name, contact.Phone);
			saveSharedPreferences(SetofMyContacts, contact.Name, contact.Phone); //this line might be bugged!
		}
		
		public void addCallCard(CallCard callcard){
			saveSharedPreferences(AllofMyCallCards, callcard.Pre, callcard.PIN);
			saveSharedPreferences(SetofMyCallCards, callcard.Pre, callcard.PIN);
		}
		
		public void setDefaultCallCard(CallCard callcard){
			saveSharedPreferences(MyDefaultCallCard, callcard.Pre, callcard.PIN);
		}
		
		public int NumberofMyContacts(){
			return Arrays.asList(loadSharedPreferencesSet(SetofMyContacts,Name_Set).toArray()).size();    //could be bugs
		}
		
		public int NumberofMyCallCards(){
			return Arrays.asList(loadSharedPreferencesSet(SetofMyCallCards,Card_Set).toArray()).size();
		}
		
		public Button presentAsButtons(int i, String type){
			Button button = new Button(getApplicationContext());
			if (type == SetofMyContacts){
				List namelist = Arrays.asList(loadSharedPreferencesSet(SetofMyContacts,Name_Set).toArray());
				String name = loadSharedPreferences(AllofMyContacts, (String) namelist.get(i));
				name = name.replaceAll("\\d+", "");
				button.setText(name);
			}
			if (type == SetofMyCallCards){
				List cardlist = Arrays.asList(loadSharedPreferencesSet(SetofMyCallCards,Card_Set).toArray());
				String card = loadSharedPreferences(AllofMyCallCards, (String) cardlist.get(i));
				button.setText(card);
			}
			return button;
		}
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pick);
		final SPMethods spm = new SPMethods();
		LinearLayout picklayout = (LinearLayout) findViewById(R.id.picklayout);
		picklayout.setOrientation(LinearLayout.VERTICAL);
		Button delete = (Button) findViewById(R.id.deletethiscontact);
		//testing
		final String Info = getIntent().getExtras().getString("Info");
		TextView testing = (TextView) findViewById(R.id.Info);
		testing.setText(Info);
		//test complete!
		delete.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				spm.removeContact(Info);
				finish();
			}
		});
		
		for (int i=0; i < spm.NumberofMyCallCards(); i++){
			Button button = spm.presentAsButtons(i, spm.SetofMyCallCards);
			picklayout.addView(button);
			List cardlist = Arrays.asList(spm.loadSharedPreferencesSet(spm.SetofMyCallCards,spm.Card_Set).toArray());
			final String card = spm.loadSharedPreferences(spm.AllofMyCallCards, (String) cardlist.get(i));
			button.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					String number = "tel:" + card + Info.replaceAll("\\D+", "");
					Intent tonext = new Intent(PickCardActivity.this, FinalCardActivity.class);
					tonext.putExtra("CardInfo", card);
					tonext.putExtra("Number", number);
					startActivity(tonext);
				}
			});
		}
	}
}
